from django.db import models
from ckeditor.fields import RichTextField


# Create your models here.


class Products(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField('产品名称', max_length=128, null=False, default='')
    item_num = models.CharField('货号', max_length=32, null=False, default='', unique=True)
    uni_prot = models.CharField('UniProt', max_length=32, null=False, default='')
    antibody_type = models.CharField('抗体类型', max_length=128, null=False, default='')
    molecular_weight = models.CharField('分子量', max_length=64, null=False, default='')
    positive_part = models.CharField('阳性部位', max_length=64, null=False, default='')
    pretreatment = models.CharField('预处理', max_length=128, null=False, default='')
    clone_num = models.CharField('克隆号', max_length=32, null=False, default='')
    storage_validity_period = models.CharField('存储条件及有效期', max_length=128, null=False, default='')
    description = models.TextField('描述', null=False, default='')
    experimental_program = models.TextField('实验方案', null=False, default='')
    remarks = models.CharField('备注', max_length=255, null=False, default='')
    product_images_list = models.TextField('图片列表', null=True, default='')
    product_guide_book = models.TextField('使用说明书', null=True, default='')


class Orders(models.Model):
    id = models.AutoField(primary_key=True)
    product_item_num = models.CharField('货号', max_length=32, null=False, default='')
    sub_item_num = models.CharField('子货号', max_length=32, null=False, default='')
    flavor = models.CharField('规格', max_length=32, null=False, default='')
    unit = models.CharField('单位', max_length=32, null=False, default='')
    product_type = models.CharField('产品类型', max_length=32, null=False, default='')


class Partners(models.Model):
    id = models.AutoField(primary_key=True)
    partners = models.CharField(verbose_name="合作伙伴名称", max_length=200, unique=True)
    description = models.TextField(verbose_name="描述", null=True, blank=True)
    partners_logo = models.CharField(verbose_name="下载图片地址", max_length=100, null=True, blank=True)
    partners_url = models.CharField(verbose_name="合作伙伴网址", max_length=100)
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)


class News(models.Model):
    TYPE_CHOICES = [
        ('performance', '业绩信息'),
        ('project_update', '工程动态'),
    ]

    title = models.CharField('标题', max_length=200, null=False)
    publish_date = models.DateField('发布日期', null=False)
    content = RichTextField('内容', null=False)
    cover_image_url = models.CharField('封面图片URL', max_length=255, null=True, blank=True)
    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    view_count = models.PositiveIntegerField('查看人数', default=0)
    type = models.CharField('类型', max_length=20, choices=TYPE_CHOICES, default='performance')

    def __str__(self):
        return self.title

    def increment_view_count(self):
        self.view_count += 1
        self.save(update_fields=['view_count'])

class Honour(models.Model):
    TITLE_CHOICES = [
        ('qualification', '资质证书'),
        ('enterprise_honour', '企业荣誉'),
    ]

    title = models.CharField('标题', max_length=200, null=False)
    image_url = models.CharField('荣誉图片', max_length=255, null=True, blank=True)
    honour_type = models.CharField('类型', max_length=20, choices=TITLE_CHOICES, default='qualification')
    description = models.TextField('描述', null=True, blank=True)  # 描述字段可以为空
    award_date = models.DateField('授予时间')  # 日期字段

    def __str__(self):
        return f"{self.get_honour_type_display()} - {self.title}"

    class Meta:
        verbose_name = "荣誉"
        verbose_name_plural = "荣誉列表"


class Project(models.Model):
    # 定义项目状态的选择项
    STATUS_CHOICES = [
        ('construction', '施工中'),
        ('accepted', '已验收'),
        ('not_accepted', '未验收'),
        ('overdue_accepted', '工期已过已验收'),
        ('abandoned', '放弃中标'),
    ]

    name = models.CharField('项目名称', max_length=200, null=False)
    manager = models.CharField('项目经理', max_length=100, null=False)
    bid_amount = models.DecimalField('中标金额', max_digits=10, decimal_places=2, null=False)
    duration = models.IntegerField('工期（天）', null=False)
    status = models.CharField('工程状态', max_length=20, choices=STATUS_CHOICES, default='construction')
    content = models.TextField('施工内容', null=True, blank=True)
    image_url = models.CharField('中标图片', max_length=255, null=True, blank=True)
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "项目"
        verbose_name_plural = "项目列表"
